const role = require('../model/role')
const { handleError } = require('../untils/error')

/**
 * @api {post} /createrole 创建角色
 * @apiName 创建角色
 * @apiGroup 角色管理
 * @apiVersion  1.0.0
 * 
 * @apiParam {String} name 角色名
 * 
 * @apiParamExample  {type} 请求示例:
  {
    name: "测试222"
  }
 * 
 * 
 * @apiSuccessExample {type} 返回示例:
  {
      "message": "添加角色成功",
      "code": 0
  }
 */
const role_create = async (ctx, next) => {
  const {
    name
  } = ctx.request.body
  try {
    const findRole = await role.findOne({
      name
    })
    console.log(findRole);
    if (findRole) {
      ctx.body = {
        message: '角色存在',
        code: -1
      }
    } else {
      const createRole = await role.create({
        name
      })
      console.log(createRole);
      if (createRole !== null) {
        ctx.body = {
          message: '添加角色成功',
          code: 0
        }
      }
    }
  } catch (error) {
    ctx.body = {
      message: '添加角色失败：' + error,
      code: -1
    }
  }
};

/**
 * @api {post} /removerole 删除角色
 * @apiName 删除角色
 * @apiGroup 角色管理
 * @apiVersion  1.0.0
 * 
 * @apiParam {String} name 角色名
 * 
 * @apiParamExample  {type} 请求示例:
  {
    name: "测试222"
  }
 * 
 * 
 * @apiSuccessExample {type} 返回示例:
  {
      success: true,
      message: '删除成功',
      code: 0
  }
 */
const role_remove = async (ctx, next) => {
  const {
    name
  } = ctx.request.body
  try {
    const removeRole = await role.remove({name});
    if (removeRole.deletedCount >= 1) {
      ctx.body = {
        success: true,
        message: '删除成功',
        code: 0
      }
    } else {
      ctx.body = {
        success: false,
        message: '删除失败',
        code: -1
      }
    }
  } catch (error) {
    ctx.body = {
      success: false,
      message: '删除角色失败：' + error,
      code: -1
    }
  }
};

/**
 * @api {post} /updaterole 角色更新
 * @apiName 角色更新
 * @apiGroup 角色管理
 * @apiVersion  1.0.0
 * 
 * @apiParam {String} name 要修改的用户名
 * @apiParam {String} updateName 修改后的用户名
 * 
 * @apiSuccess (200) {string} _id 角色id
 * @apiSuccess (200) {string} name 被修改的角色名
 * @apiSuccess (200) {string} registerTime 注册时间
 * 
 * @apiParamExample  {type} 请求示例:
  {
    name: "测试124"
    updateName: "测试1额"
  }
 * 
 * 
 * @apiSuccessExample {type} 返回示例:
  {
    "data": {
        "_id": "60dd9b16f8dde028a5380257",
        "name": "测试124",
        "registerTime": "2021-07-01T10:38:14.964Z",
        "__v": 0
    },
    "message": "角色更新成功",
    "code": 0
  }
 */
const role_update = async (ctx, next) => {
  try {
    const {
      name,
      updateName
    } = ctx.request.body
    const updateRole = await role.findOneAndUpdate({name},{name: updateName})
    if (updateRole) {
      ctx.body = {
        data: updateRole,
        message: "角色更新成功",
        code: 0,
      };
    } else {
      ctx.body = {
        data: null,
        message: "角色更新失败",
        code: -1,
      };
    }
  } catch (error) {
    ctx.body = {
      message: '更新角色失败：' + error,
      code: -1
    }
  }
};

/**
 * @api {get} /findrole 角色查询
 * @apiName 角色查询
 * @apiGroup 角色管理
 * @apiVersion  1.0.0
 * 
 * @apiSuccess (200) {string} _id 角色id
 * @apiSuccess (200) {string} name 被修改的角色名
 * @apiSuccess (200) {string} registerTime 注册时间
 * 
 * @apiSuccessExample {type} 返回示例:
 {
    "success": true,
    "data": [
        {
            "_id": "60dd7266267bcd1ad9fe2657",
            "name": "测试test",
            "registerTime": "2021-07-01T07:44:38.783Z",
            "__v": 0
        },
        {
            "_id": "60dd78e1267bcd1ad9fe2693",
            "name": "admin",
            "registerTime": "2021-07-01T08:12:17.756Z",
            "__v": 0
        },
        {
            "_id": "60dd91c9f8dde028a53801e6",
            "name": "测试2",
            "registerTime": "2021-07-01T09:58:33.493Z",
            "__v": 0
        },
    ],
    "message": "查询角色成功",
    "code": 0
}
 */
const role_find = async (ctx, next) => {
  try {
    const findRole = await role.find()
    if (findRole.length > 0) {
      ctx.body = {
        success: true,
        data: findRole,
        message: '查询角色成功',
        code: 0
      }
    } else {
      tx.body = {
        success: false,
        data: null,
        message: '没有角色',
        code: -1
      }
    }
  } catch (error) {
    ctx.body = {
      success: false,
      message: '查询角色失败：' + error,
      code: -1
    }
  }
};

module.exports = {
  'POST /createrole': role_create,
  'POST /removerole': role_remove,
  'POST /updaterole': role_update,
  'GET /findrole': role_find
};